<?php
/**
 * Introduction: 优惠卷
 * @author: 杨陈鹏
 * @email: wanglong@cdlinglu.com
 */

namespace App\Controller\Member;


use Common\Controller\App\SignController;
use Common\Logic\VolumeLogic;

class VolumeController extends SignController
{

    /**
     * Introduction: 优惠卷列表
     * @author: 杨陈鹏
     * @email: wanglong@cdlinglu.com
     */

    public function lists()
    {
        $page = I('post.page',1);
        $nums = I('post.nums',20);

        $data = I('post.data');

        $type = $data['type'];

        //检测是否是结算时获取的商品
        $checkout   = $data['checkout'] ? $data['checkout'] : false ;
        $goods_ids  = $data['products'];

        $where = 'uid = '.self::$uid;
        //未使用
        if($type == 1)
            $where .= " AND status = 1 AND end_time >= ".NOW_TIME;

        if($type == 2)
            $where .= " AND status = 2";

        if($type == 3)
            $where .= " AND status = 1 AND end_time < ".NOW_TIME;

        $logic = new VolumeLogic();

        outPut($logic::getData($where,$page,$nums,'*',$checkout,$goods_ids));
    }
    /**
     * Introduction: 优惠卷转赠
     * @author: 杨陈鹏
     */
    public function receive()
    {

        $data       =   I('post.data');
        $code       =   $data['code'];

        $logic      =   new VolumeLogic();

        $old_volume =   $logic::$model->where(['code' => $code])->find();

        if( $old_volume['is_receive'] == 2)
            outPut(['code' => 300, 'msg' => '该优惠卷已经转赠过!']);

        if( !$old_volume )
            outPut(['code' => 300, 'msg' => '优惠卷不存在!']);

        if( $old_volume['status'] == 2 )
            outPut(['code' => 300, 'msg' => '优惠卷已被使用!']);

        if( $old_volume['end_time'] < NOW_TIME )
            outPut(['code' => 300, 'msg' => '优惠卷已过期!']);

        $result =   $logic::$model
            ->where(['code' => $code])
            ->save([
                'uid'           => self::$uid,
                'from_id'       => $old_volume['uid'],
                'from_time'     => NOW_TIME,
                'is_receive'    => 2,
            ]);

        if ( $result !== false )
            outPut(['code' => 200, 'msg' => '转赠成功!']);

        outPut(['code' => 300, 'msg' => '转赠失败!']);


    }

}